package com.taxigang.client;

import com.allen_sauer.gwt.log.client.Log;



public class Authenication {

	public enum Provider { Google, Facebook , TEMP};
	public enum Status { LoggedIn, LoggedOut, TimedOut, Unknown };
	public enum AuthStatus { LoggedInGoogle, LoggedInFacebook, NotLoggedIn, LoggedInTmp };
	
	//TODO assuming google logged out

	private Status googleStatus = Status.LoggedOut;
	private Status facebookStatus = Status.Unknown;
	private Status tmpStatus = Status.Unknown;
	private Provider provider;
	private static Authenication instance = new Authenication();
	
	private Authenication(){
		
	}
	public void reset(){
		googleStatus = Status.Unknown;
		facebookStatus = Status.Unknown;		
		tmpStatus = Status.Unknown;		
	}

	public Provider getProvider(){
		return provider;
	}

	public static Authenication getInstance(){
		return instance;
	}

	/**
	 * Return true if the caller can assume that you are logged in on this authentication provider
	 * Assume to be logged in to the first provider to respond. That is, if a subsequent provider 
	 * responds with a logged in confirmation then return false
	 * @param provider
	 * @param status
	 * @return
	 */
	public boolean getLogedinResolved(Provider provider, Status status){
		Log.debug("getLogedinResolved="+this.hashCode());
		
		switch (provider){
		case Google:
			googleStatus = status; 
			break;
		case Facebook:
			facebookStatus = status; 
			break;
		case TEMP:
			tmpStatus = status; 
			break;
		}
		
		boolean resolved = false;
		if (status == Status.LoggedIn){
			resolved = true;
			this.provider=provider;
		} else {
			switch (provider){
			case Google:
				resolved = (facebookStatus != Status.Unknown);
				break;
			case Facebook:
				resolved =  (googleStatus != Status.Unknown);
				break;
			}
		}
		
		Log.debug("Provider="+provider+ " status="+status+ " resolved="+resolved);
		return resolved;
	}
	
	/**
	 * Return true if the caller can assume that you are logged out on all authentication  providers.
	 * Must wait until all have answered.
	 * @param provider
	 * @param status
	 * @return
	 */
	public boolean getLoggedoutResolved(Provider provider, Status status){

		//Log.info("getLoggedoutResolved:"+provider+":"+status);
		switch (provider){
		case Google:
			googleStatus = status; 
			break;
		case Facebook:
			facebookStatus = status; 
			break;
		}

		//Log.info("getLoggedoutResolved:facebookStatus="+facebookStatus);
		//Log.info("getLoggedoutResolved:googleStatus="+googleStatus);
		boolean resolved = googleStatus== Status.LoggedOut && (facebookStatus== Status.LoggedOut || facebookStatus== Status.TimedOut);
		//Log.info("getLoggedoutResolved:resolved:"+resolved);
		return resolved;
	}
	
	public AuthStatus getAuthStatus(){
		AuthStatus status = AuthStatus.NotLoggedIn;
		if (googleStatus == Status.LoggedIn){
			status = AuthStatus.LoggedInGoogle;
		}
		if (facebookStatus == Status.LoggedIn){
			status = AuthStatus.LoggedInFacebook;
		}
		if (tmpStatus == Status.LoggedIn){
			status = AuthStatus.LoggedInTmp;
		}
		
		return status;
	}
	
	public void setAuthStatus(Provider provider, Status status){
		switch(provider){
		case Google:
			googleStatus = status;
			break;
		case Facebook:
			facebookStatus = status;
			break;
		case TEMP:
			tmpStatus = status;
			break;
		}
	}
}
